/**
 * Created by AlenZhon on 2017/7/14.
 */
$().ready(function () {
    //localStorage.clear();
    'use strict';
    function repeat(name){      //检查用户名是否存储在localstorage
        for (var i=0;i<localStorage.length;i++) {  //调用key方法获取localStorage中数据对应的键名
            //localStorage.key(0)对应data1
            var getKey = localStorage.key(i);
            var str = localStorage.getItem(getKey);
            var data = JSON.parse(str);
            if (name === data.username) {
                return data.password;       //如果用户名存在则返回密码
            }
        }
        return false;
    }
    $("div :input").blur(function () {
        var $parent = $(this).parent().parent();
        $parent.find("div[id*=tips]").remove();  //删除以前的提醒元素
        //验证用户名
        if ($(this).is('#username')){
            if (!repeat(this.value)){
                var errorMsg= "该用户名不存在";
                $parent.attr("class","weui-cell weui-cell_warn");
                $parent.append('<div class="weui-cell__ft" id="tips onerror"><i class="weui-icon-warn"><span class="weui-check__label">'+errorMsg+'</span></i>></div>');
            }
            else {      //因为密码一定非空 则如果返回了非空值则表明查找到了
                $parent.attr("class","weui-cell weui-cell");
                $parent.append('<div class="weui-cell__ft" id="tips onsuccess"><i class="weui-icon-success-circle"></i>></div>');
            }
        }
        if ($(this).is("#psw")) {
            var namecheck=$('#username').val();     //获取用户名进行检查
            if (this.value!==repeat(namecheck)){
                errorMsg= "密码错误";
                $parent.attr("class","weui-cell weui-cell_warn");
                $parent.append('<div class="weui-cell__ft" id="tips onerror"><i class="weui-icon-warn"><span class="weui-check__label">'+errorMsg+'</span></i>></div>');
            }
            else {
                $parent.attr("class","weui-cell weui-cell");
                $parent.append('<div class="weui-cell__ft" id="tips onsuccess"><i class="weui-icon-success-circle"></i>></div>');
            }
        }
    }).keyup(function () {          //改变文本框的值能即时提醒
        $(this).triggerHandler("blur");
    }).focus(function () {          //定位到文本框上能即时提醒
        $(this).triggerHandler("blur");
    });

    $('#logcheck').click(function(){    //对表单进行整体验证
        $("weui-input").trigger('blur');
        var numerror= $("div[id*=onerror]").length;
        if (numerror){   //表示填写信息有错
            return false;
        }
        else {
            $.toast("登录成功",function () {
                console.log('close');
                for (var i=0;i<localStorage.length;i++) {  //寻找本地存储中该用户的信息
                    var getKey = localStorage.key(i);
                    var str = localStorage.getItem(getKey);
                    var data = JSON.parse(str);
                    if (data.username===$("#username").val()) {
                        data.stat=1;       //用stat一项表示用户是否登录
                        data.str= JSON.stringify(data);
                        localStorage.setItem("userID"+data.username,data.str);
                    }
                }
                window.location.href = "Main.html";    //登录
            })
        }
    });
});